<!-- 2020年2月15日14:57:57 完成 -->
<template>
	<view class="padding-top-xs">
		<view class="edgeInsetTop"></view>
		<view class="tips">
			<text class="iconfont icon-jinggao"></text>
			<view class="text">
				<text>修改后会影响物流时效、送货上门等服务，只能原价修改且付款后只能修改一次。</text>
				<text>若因商品换仓、已发货、运费变更等原因导致修改失败，请您谅解。</text>
			</view>
		</view>
		
		<view class="old flex flex-direction padding-lr margin-top margin-bottom-sm" v-if="orderData">
			<text>原地址：</text>
			<text class="address">{{orderData.address.address}}</text>
			<text class="name">{{orderData.address.name}} {{orderData.address.mobile}}</text>
		</view>
		
		<view class="margin-top-bj">
			<view class="cu-list menu-avatar margin-lr bg-white" style="border-radius: 10rpx;">
				<view class="header">
					<text>选择新的收货地址</text>
					<view class="add" @tap="addAddress('add')">
						<text>添加新地址</text>
						<text class="iconfont icon-zengjia"></text>
					</view>
				</view>
				<text>近期使用：</text>
				<wanl-empty v-if="addressList.length === 0 && status !== 'loading'" text="还没添加任何地址"/>
				<view class="cu-item" :class="modalName == 'move-box-' + index ? 'move-cur' : ''" v-if="addressList" v-for="(item, index) in addressList" :key="item.id" @tap="checkAddress(item)" >
					<view class="cu-avatar round lg" style="background-color: #C2F3CD;">{{ item.name.substring(0, 1) }}</view>
					<view class="content">
						<view>
							<text class="wanl-pip margin-right-sm text-lg">{{ item.name }}</text>
							<text class="wanl-gray-light text-sm">{{ item.mobile }}</text>
						</view>
						<view class="text-sm wanl-pip text-cut-2" style="line-height: 1.3;">
							<text class="wlIcon-dizhi text-red margin-right-xs" v-if="item.default == 1"></text>
							{{ item.address_detail }} 
						</view>
					</view>
					<view class="action" @tap.stop="addAddress('edit', item)"><text class="wlIcon-xuanze text-l text-green"></text></view>
				</view>
				<wanl-empty text="没有找到任何地址" src="address_default3x" v-else/>
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText" />
		<view class="edgeInsetBottom"></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			source: 0,
			order_id: 0,
			addressList: [],
			modalName: null,
			listTouchStart: 0,
			listTouchDirection: null,
			//上拉下拉
			total: 0, //数据量
			current_page: 1, //当前页码
			last_page: 1 ,//总页码
			status: 'loading',
			contentText: {
				contentdown: ' ',
				contentrefresh: '正在加载..',
				contentnomore: ''
			},
			orderData:null
		};
	},
	// 监听下拉刷新
	onPullDownRefresh() {
		this.current_page = 1;
		this.reload = true;
		this.loadData();
	},
	// 监听上拉加载
	onReachBottom() {
		//判断是否最后一页
		if(this.current_page >= this.last_page){
			this.status = 'noMore';
		}else{
			this.current_page = this.current_page + 1; //页码+1
			this.status = 'loading';
			this.loadData(true);
		}
	},
	onLoad(option) {
		this.source = option.source;
		this.order_id = option.order_id;
		this.loadData();
		this.getOrderInfo({id:option.order_id});
	},
	methods: {
		// 获取订单
		async getOrderInfo(data) {
			await uni.request({
				url: '/wanlshop/order/getOrderInfo',
				data: data,
				success: res => {
					this.orderData = res.data;
				}
			});
		},
		async loadData(reload, refresh) {
			await uni.request({
				url: '/member/address/lists',
				data:{
					page: reload?(this.current_page):1
				},
				success: res => {
					if(reload){
						this.addressList = this.addressList.concat(res.data.data);
					}else{
						this.addressList = res.data.data;
						uni.stopPullDownRefresh();
					}
					this.total = res.data.total; //数据量
					this.current_page = res.data.current_page; //当前页码
					this.last_page = res.data.last_page; //总页码
					this.status = res.data.total == 0 ? 'noMore':'more';
				}
			});
		},
		// 删除
		async delAddress(id, key) {
			this.$delete(this.addressList, key); //vue方法
			await uni.request({
				url: '/publics/address/deladdress',
				method: 'POST',
				data: { id: id }
			});
		},
		//添加或修改成功之后回调
		async refreshList(data, type) {
			await uni.request({
				url: `/member/address/${type}`,
				method: 'POST',
				data: data,
				success: (res) => {
					this.loadData(false,true);
				}
			});
		},
		//选择地址
		checkAddress(item) {
			if (this.source == 1) {
				this.$wanlshop.prePage().loadData(item.id);
				this.$wanlshop.back(1);
			}else if(this.source == 2){ //我的订单修改地址  地址ID   返回订单ID
				this.$wanlshop.prePage().refreshList(item.id, this.order_id);
				this.$wanlshop.back(1);
			}
		},
		// 新增地址
		addAddress(type, item) {
			this.$wanlshop.to(`/pages/user/envelope/addressManage?type=${type}&data=${JSON.stringify(item)}`);
		},
		// ListTouch触摸开始
		ListTouchStart(e) {
			this.listTouchStart = e.touches[0].pageX;
		},
		// ListTouch计算方向
		ListTouchMove(e) {
			this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left';
		},
		// ListTouch计算滚动
		ListTouchEnd(e) {
			if (this.listTouchDirection == 'left') {
				this.modalName = e.currentTarget.dataset.target;
			} else {
				this.modalName = null;
			}
			this.listTouchDirection = null;
		}
	}
};
</script>

<style lang="less">
	page {
		background-color: #EDEDED;
	}
.cu-avatar.lg {
	width: 90rpx;
	height: 90rpx;
	font-size: 1.5em;
	color: #4CBD66;
}
.cu-list.menu-avatar>.cu-item{
	height: 160rpx;
	&::after {
		border: none;
	}
	.cu-avatar {
		left: 18rpx;
		width: 76rpx;
		height: 76rpx;
	}
	.content {
		left: 118rpx;
	}
}

.wanlian.cu-bar.tabbar {
	background-color: transparent;
}
.wanlian.cu-bar.tabbar .cu-btn {
	width: calc(100% - 50rpx);
	border-radius: 50rpx;
}
.wanlian.cu-bar.tabbar .cu-btn.lg {
	font-size: 32rpx;
	height: 86rpx;
}
.cu-list {
	.header {
		padding: 24rpx 18rpx 16rpx 18rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		> text {
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 44rpx;
		}
		view {
			text {
				color: #666666;
				&:nth-child(1) {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					line-height: 40rpx;
				}
				&:nth-child(2) {
					font-size: 26rpx;
					font-weight: bold;
					margin-left: 6rpx;
				}
			}
		}
	}
	> text {
		padding: 0 18rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #888888;
		line-height: 34rpx;
	}
}
.tips {
	display: flex;
	justify-content: flex-start;
	align-items: center;
	padding: 0 32rpx;
	height: 140rpx;
	background-color: #F7F7F7;
	> text {
		font-size: 54rpx;
		color: #F10F0F;
		font-weight: bold;
		margin-right: 8rpx;
	}
	.text {
		text {
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			line-height: 28rpx;
			&:nth-child(1) {
				color: #F10F0F;
			}
			&:nth-child(2) {
				color: #888888;
			}
		}
	}
}

.old {
	text {
		&:nth-child(1) {
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 44rpx;
			margin-bottom: 22rpx;
		}
		&:nth-child(2) {
			font-size: 30rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
		}
		&:nth-child(3) {
			font-size: 28rpx;
			color: #555555;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
		}
	}
}
</style>
